/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 

#ifdef ECOS
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "drv_api.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#include "NUC900_reg.h"
#else
#include <stdio.h>
#include "NUC900_GE.h"
#include "wbio.h"
#ifdef _NUC900_
#include "NUC900_reg.h"
#else
#include "reg702.h"
#endif
#endif


#define ABS(x)		(((x)>0)?(x):-(x))
#define MAX(a,b)	(((a)>(b))?(a):(b))

/* octant code of line drawing */

#define	XpYpXl		(0<<1)   // XY octant position is 1~3 in Control register
#define	XpYpYl		(1<<1)
#define XpYmXl  	(2<<1)
#define XpYmYl  	(3<<1)
#define XmYpXl  	(4<<1)
#define XmYpYl  	(5<<1)
#define XmYmXl  	(6<<1)
#define XmYmYl  	(7<<1)

///////////////////////////////////////////////////////////////////////////////
//
// Function: draw_frame()
//
// Description:
//   Draw an one-pixel rectangle frame.
//   Draw Option : Rectangle = 0, Diagonal = 1
//   The angle of diagonal frame is 45
///////////////////////////////////////////////////////////////////////////////
void draw_frame(int x1, int y1, int x2, int y2, int color, int opt)
{
  UINT32 dest_pitch, dest_start, dest_dimension;
  UINT32 color32;
 
#ifdef DEBUG
  printf("draw_frame():\n");
  //printf("(%d,%d)-(%d,%d)\n", x1, y1, x2, y2);
  printf("color=0x%x opt=%d\n", color, opt);
#endif   

  /*
  ** The opt==1 case must be specially handled.
  */
  	
  if (opt==0)	
  {
    outpw(REG_2D_GEC, 0xcccb0000); // rectangle
  }  
  else
  {
    outpw(REG_2D_GEC, 0xcccf0000); // diagonal
    //outpw(REG_2D_GEC, 0xcccb0000); // diagonal, smf
    //x1 = x1+(x2-x1)>>1; // first to do addition and subtract, then shift value
  }  

#ifdef DEBUG 
  printf("(%d,%d)-(%d,%d)\n", x1, y1, x2, y2);
#endif

  color32 = make_color(color);  
  outpw(REG_2D_GEFC, color32); 
  
  dest_pitch = GFX_WIDTH << 16; // pitch in pixel
  outpw(REG_2D_GESDP, dest_pitch);

  dest_start = y1 << 16 | x1;
  outpw(REG_2D_GEDSXYL, dest_start);
  
  dest_dimension = (y2-y1) << 16 | (x2-x1);
  outpw(REG_2D_GEDIXYL, dest_dimension);
  
  outpw(REG_2D_GEMC, inpw(REG_2D_GEMC)); // address caculation
  
  outpw(REG_2D_GETG, 1); 
  
  while ((inpw(REG_2D_GEINTS)&0x01)==0); // wait for command complete  
  
  outpw(REG_2D_GEINTS, 1); // clear interrupt status
}

